import time

import requests
import time
DEFAULT_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'
headers = {'User-Agent': DEFAULT_USER_AGENT}

def getItemDetail(skuId, skuNum=1, areaId=1):

    url = 'https://item-soa.jd.com/getWareBusiness'
    payload = {
        'skuId': skuId,
        'area': areaId,
        'num': skuNum
    }
    resp = requests.get(url=url, params=payload, headers=headers)
    return resp

# resp = getItemDetail(10097777347233)
# print(resp)
# print(resp.text)

TEST_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0'
headers = {'User-Agent': TEST_USER_AGENT}

print(time.time())
stra = int(time.time())

url1 = 'https://api.m.jd.com/?appid=pc-item-soa&functionId=pc_detailpage_wareBusiness&client=pc&clientVersion=1.0.0&t='
url2 = '&body=%7B%22skuId%22%3A10097777347233%2C%22cat%22%3A%221315%2C1342%2C9733%22%2C%22area%22%3A%2219_1698_19830_19901%22%2C%22shopId%22%3A%2233360%22%2C%22venderId%22%3A34882%2C%22paramJson%22%3A%22%7B%5C%22platform2%5C%22%3A%5C%221%5C%22%2C%5C%22colType%5C%22%3A0%2C%5C%22specialAttrStr%5C%22%3A%5C%22p0ppppppppppppppppppppppppp%5C%22%2C%5C%22skuMarkStr%5C%22%3A%5C%2200%5C%22%7D%22%2C%22num%22%3A1%2C%22bbTraffic%22%3A%22%22%2C%22canvasType%22%3A2%7D&h5st=20240704220348152%3Bztng6im5yznnttn6%3Bfb5df%3Btk03wa6dc1c9018nCDFewXFZgbQJQNBBY4eMiMKaICko8NouQLGpjsbqYV5kcez_EVQ3RGfPCHUWUtLAWMhThYkpTkzB%3B8bd3d5d8816dac96f12d3fa642a0f6e463506add0eeb0768535201af16de0cef%3B4.7%3B1720101828152%3BTKmWpCuc4ueiDO8TQkTKeMx2QNtDDvJPgtWvE5bUS0BR-ecZBul7YiQ6FaYK6NV8kUjGEDLsSNNjxLOylG162ep0yXT920PWe9LVx_TvK48ZAyNIaf9TZvFsj4J9fw3RDa8RjXSVVIne59RUlbswuTemHc08T-Jq-IKoATdIuqkeOd_JrzwfCl4uACRYyEZzteDjffgsDdYitqqEBxn8rOj22b1R9qdoS-tmexHeJyEpYhQIu94c-7EKuIUZ33UKXKCP1skPN06S2JteBJahD_Xm-tmxthLyFQzfFNWtYbv2dTKsrgyfIv7ViE5Ri1tyqE5QXi7obskYmqYALSXnYPBSCMzIy1UPr05ByNB_C4UHNaT8fY_p5xIpUsrVxOLCu7nZggE7nDk8PeheJO0dl8zjLad9Prk3hGJ0DQIeqffFGvzEemLTD52YgeDqWQHLXbk3&x-api-eid-token=jdd03AKGMT3G54JLFWAHIW55EB6CBPLP7QXCC7YJS2B5KY6FZFRSWAM36NMNGYK5H6XXM6KI6PAH55BZCXJ2DMORGAXXAAQAAAAMQPYHGSWAAAAAADOX3X6JTSXGXFAX&loginType=3&scval=10097777347233&uuid=181111935.1719726679268997885868.1719726679.1719729124.1720099282.3'

# resp = requests.get(url = url1 + str(int(time.time())) + url2,headers=headers)

# print(resp)

# -*- coding: UTF-8 -*-

import requests,json
import pkgutil
import time
from urllib.parse import urlparse, parse_qs, urlunparse
import hashlib
import execjs
from urllib.parse import quote
import io
import sys

def savetofile(text,filename):
    file = open(filename, "w",encoding='utf-8' )
    file.write(text)
    file.close()
def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')# Press Ctrl+F8 to toggle the breakpoint.
def jd(skuid):
    appid='item-v3'
    functionId='recDivinerApi'
    body={"lid":27,"lim":15,"ec":"utf-8","uuid":"16900368971511636315768","pin":"","p":902029,"sku":skuid,"ck":"pin,ipLocation,atw,aview","c1":1316,"c2":1387,"c3":11932,"securityToken":"iJJJBrR7BAxWWavOluQxmMQ","clientChannel":"3","clientPageId":"item.jd.com"}
    js_file = open("h5st.js", "r", encoding='utf-8')
    js=js_file.read()
    exc = execjs.compile(js)
    url= exc.call("sign", appid,functionId,body)
    print('url='+url)
    headers={
    "Authority": "api.m.jd.com",
    "Accept": "application / json, text / javascript, * / *; q = 0.01",
    "Accept - Encoding": "gzip, deflate, br",
    "Accept - Language": "zh - CN, zh;q = 0.9",
    "Cookie": "shshshfpb=i0ZU6VlHi9tt1RukWDDyR0w; 3AB9D23F7A4B3C9B=GZSZ6SPDPJZS6ARBGAUDIS7NMVC2A24XK6SN4JCWH44HGMYJVGXZIEY2SHDTJKNBR32WP5NA7JKC4CLDZDF5AIRXNA; shshshfpa=cb3af5e3-c2cf-dae5-48e3-c2331a38092a-1653253655; shshshfpx=cb3af5e3-c2cf-dae5-48e3-c2331a38092a-1653253655; __jdc=122270672; __jdv=122270672|direct|-|none|-|1689305241830; __jdu=16893052418291576334291; areaId=25; ipLoc-djd=25-2258-2261-6568; token=7a3a5010c8ea7250057d9168270daacd,2,939221; __tk=be32047e11adf495830ad564f7c34cd6,2,939221; 3AB9D23F7A4B3CSS=jdd03GZSZ6SPDPJZS6ARBGAUDIS7NMVC2A24XK6SN4JCWH44HGMYJVGXZIEY2SHDTRiDY9CRQSU93J9SUTiPmFy3PTP7N8itsNd7DLuiPzfoEjAAACXCBKUWUQMP7FMX; _gia_d=1; jsavif=1; __jda=122270672.16893052418291576334291.1689305242.1690550636.1690599310.7; __jdb=122270672.1.16893052418291576334291|7.1690599310",
    "Origin": "https://item.jd.com",
    "Referer": "https://item.jd.com/",
    "Sec-Ch-Ua": "\"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Google Chrome\";v=\"114\"",
    "Sec-Ch-Ua-Mobile":"?0",
    "Sec-Ch-Ua-Platform":"\"Windows\"",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-site",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
    "X-Referer-Page": f"https://item.jd.com/{skuid}.html",
    "X-Rp-Client": "h5_1.0.0",
    }
    res=requests.get(url=url, headers=headers)
    print(res)
    text=res.text
    savetofile(text,"sku.txt")
    print(text)
    return text
# Press the green button in the gutter to run the script.

if __name__ == '__main__':
    print_hi('新4.1版本h5st签名返回商品详情。技术支持：byc6352')
    jd(100019322424)



